#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Created: Tue May 18 19:08:22 2010

from sys import argv
from PyQt4 import QtCore, QtGui

class FrameUserAdmin(QtGui.QFrame):
    def __init__(self, connection, parent = None):
        super(FrameUserAdmin, self).__init__(parent)
        """setup Interface"""
        self.setupUi(self)
        
        self.connection = connection
        self.userAdmin = self.connection.root.Modules.UserAdmin.UserAdmin()
        groupsList = self.userAdmin.listAllGroups()
        for group in groupsList:
            self.combo_defaultGroup.addItem(group)
        
    def setupUi(self, User_admin_frame):
        User_admin_frame.setObjectName("User_admin_frame")
        User_admin_frame.resize(819, 580)
        User_admin_frame.setGeometry(QtCore.QRect(210, 45, 831, 581))
        User_admin_frame.setFrameShape(QtGui.QFrame.StyledPanel)
        User_admin_frame.setFrameShadow(QtGui.QFrame.Raised)
        self.gridLayout_5 = QtGui.QGridLayout(User_admin_frame)
        self.gridLayout_5.setObjectName("gridLayout_5")
        self.gridLayout = QtGui.QGridLayout()
        self.gridLayout.setObjectName("gridLayout")
        self.btn_showAllUsers = QtGui.QPushButton(User_admin_frame)
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap("./icons/preferences-desktop-user.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.btn_showAllUsers.setIcon(icon)
        self.btn_showAllUsers.setObjectName("btn_showAllUsers")
        self.gridLayout.addWidget(self.btn_showAllUsers, 0, 0, 1, 1)
        spacerItem = QtGui.QSpacerItem(218, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
        self.gridLayout.addItem(spacerItem, 0, 1, 1, 1)
        self.listWidget_users = QtGui.QListWidget(User_admin_frame)
        self.listWidget_users.setObjectName("listWidget_users")
        self.listWidget_users.setFixedSize(500, 580)
        self.gridLayout.addWidget(self.listWidget_users, 1, 0, 1, 2)
        self.gridLayout_5.addLayout(self.gridLayout, 0, 0, 3, 1)
        self.groupBox = QtGui.QGroupBox(User_admin_frame)
        self.groupBox.setObjectName("groupBox")
        self.gridLayout_2 = QtGui.QGridLayout(self.groupBox)
        self.gridLayout_2.setObjectName("gridLayout_2")
        self.btn_delUser = QtGui.QPushButton(self.groupBox)
        icon1 = QtGui.QIcon()
        icon1.addPixmap(QtGui.QPixmap("./icons/edit-bomb.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.btn_delUser.setIcon(icon1)
        self.btn_delUser.setObjectName("btn_delUser")
        self.gridLayout_2.addWidget(self.btn_delUser, 0, 0, 1, 1)
        self.chkbx_delUserHome = QtGui.QCheckBox(self.groupBox)
        self.chkbx_delUserHome.setObjectName("chkbx_delUserHome")
        self.gridLayout_2.addWidget(self.chkbx_delUserHome, 0, 1, 1, 2)
        self.btn_prfmAction = QtGui.QPushButton(self.groupBox)
        icon2 = QtGui.QIcon()
        icon2.addPixmap(QtGui.QPixmap("./icons/preferences-desktop-user-password.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.btn_prfmAction.setIcon(icon2)
        self.btn_prfmAction.setObjectName("btn_prfmAction")
        self.gridLayout_2.addWidget(self.btn_prfmAction, 1, 0, 1, 1)
        self.rdio_lock = QtGui.QRadioButton(self.groupBox)
        self.rdio_lock.setObjectName("rdio_lock")
        self.gridLayout_2.addWidget(self.rdio_lock, 1, 1, 1, 1)
        self.rdio_unlock = QtGui.QRadioButton(self.groupBox)
        self.rdio_unlock.setObjectName("rdio_unlock")
        self.gridLayout_2.addWidget(self.rdio_unlock, 1, 2, 1, 1)
        self.gridLayout_5.addWidget(self.groupBox, 0, 1, 1, 1)
        self.groupBox_2 = QtGui.QGroupBox(User_admin_frame)
        self.groupBox_2.setObjectName("groupBox_2")
        self.gridLayout_3 = QtGui.QGridLayout(self.groupBox_2)
        self.gridLayout_3.setObjectName("gridLayout_3")
        self.label_2 = QtGui.QLabel(self.groupBox_2)
        self.label_2.setObjectName("label_2")
        self.gridLayout_3.addWidget(self.label_2, 0, 0, 1, 1)
        self.text_userName = QtGui.QLineEdit(self.groupBox_2)
        self.text_userName.setObjectName("text_userName")
        self.gridLayout_3.addWidget(self.text_userName, 0, 1, 1, 1)
        self.label_3 = QtGui.QLabel(self.groupBox_2)
        self.label_3.setObjectName("label_3")
        self.gridLayout_3.addWidget(self.label_3, 1, 0, 1, 1)
        self.text_password = QtGui.QLineEdit(self.groupBox_2)
        self.text_password.setEchoMode(QtGui.QLineEdit.Password)
        self.text_password.setObjectName("text_password")
        self.gridLayout_3.addWidget(self.text_password, 1, 1, 1, 1)
        
        self.combo_defaultGroup = QtGui.QComboBox(self.groupBox_2)
        self.combo_defaultGroup.setObjectName("text_defaultGroup")
        self.gridLayout_3.addWidget(self.combo_defaultGroup, 2, 1, 1, 1)
        
        self.chkbx_defGroup = QtGui.QCheckBox(self.groupBox_2)
        self.chkbx_defGroup.setObjectName("chkbx_defGroup")
        self.gridLayout_3.addWidget(self.chkbx_defGroup, 3, 1, 1, 1)
        self.btn_addUser = QtGui.QPushButton(self.groupBox_2)
        icon3 = QtGui.QIcon()
        icon3.addPixmap(QtGui.QPixmap("./icons/list-add-user.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.btn_addUser.setIcon(icon3)
        self.btn_addUser.setObjectName("btn_addUser")
        self.gridLayout_3.addWidget(self.btn_addUser, 4, 1, 1, 1)
        self.gridLayout_5.addWidget(self.groupBox_2, 1, 1, 1, 1)
        self.groupBox_7 = QtGui.QGroupBox(User_admin_frame)
        self.groupBox_7.setObjectName("groupBox_7")
        self.gridLayout_4 = QtGui.QGridLayout(self.groupBox_7)
        self.gridLayout_4.setObjectName("gridLayout_4")
        self.label_4 = QtGui.QLabel(self.groupBox_7)
        self.label_4.setObjectName("label_4")
        self.gridLayout_4.addWidget(self.label_4, 0, 0, 1, 1)
        self.text_userSelecedName = QtGui.QLineEdit(self.groupBox_7)
        self.text_userSelecedName.setObjectName("text_userSelecedName")
        self.gridLayout_4.addWidget(self.text_userSelecedName, 0, 1, 1, 1)
        self.label_7 = QtGui.QLabel(self.groupBox_7)
        self.label_7.setObjectName("label_7")
        self.gridLayout_4.addWidget(self.label_7, 1, 0, 1, 1)
        self.text_newPassword = QtGui.QLineEdit(self.groupBox_7)
        self.text_newPassword.setEchoMode(QtGui.QLineEdit.Password)
        self.text_newPassword.setObjectName("text_newPassword")
        self.gridLayout_4.addWidget(self.text_newPassword, 1, 1, 1, 1)
        self.btn_changePassword = QtGui.QPushButton(self.groupBox_7)
        icon4 = QtGui.QIcon()
        icon4.addPixmap(QtGui.QPixmap("./icons/applications-engineering.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.btn_changePassword.setIcon(icon4)
        self.btn_changePassword.setObjectName("btn_changePassword")
        self.gridLayout_4.addWidget(self.btn_changePassword, 2, 1, 1, 1)
        self.gridLayout_5.addWidget(self.groupBox_7, 2, 1, 1, 1)

        self.retranslateUi(User_admin_frame)
        ###################################### Signals and Slots ####################################################
        self.connect(self.btn_showAllUsers, QtCore.SIGNAL('clicked()'), self.showAllUsers)
        self.connect(self.btn_addUser, QtCore.SIGNAL('clicked()'), self.addUser)
        self.connect(self.btn_delUser, QtCore.SIGNAL('clicked()'), self.delUser)
        self.connect(self.btn_prfmAction, QtCore.SIGNAL('clicked()'), self.performAction)
        self.connect(self.btn_changePassword, QtCore.SIGNAL('clicked()'), self.changePassword)
        self.connect(self.listWidget_users, QtCore.SIGNAL('itemSelectionChanged()'), self.textChanged)
        #############################################################################################################
        QtCore.QMetaObject.connectSlotsByName(User_admin_frame)

    def retranslateUi(self, User_admin_frame):
        User_admin_frame.setWindowTitle(QtGui.QApplication.translate("User_admin_frame", "Frame", None, QtGui.QApplication.UnicodeUTF8))
        self.btn_showAllUsers.setText(QtGui.QApplication.translate("User_admin_frame", "Show all users", None, QtGui.QApplication.UnicodeUTF8))
        self.groupBox.setTitle(QtGui.QApplication.translate("User_admin_frame", "Operations", None, QtGui.QApplication.UnicodeUTF8))
        self.btn_delUser.setText(QtGui.QApplication.translate("User_admin_frame", "Delete user", None, QtGui.QApplication.UnicodeUTF8))
        self.chkbx_delUserHome.setText(QtGui.QApplication.translate("User_admin_frame", "Delete home", None, QtGui.QApplication.UnicodeUTF8))
        self.btn_prfmAction.setText(QtGui.QApplication.translate("User_admin_frame", "Perform Action", None, QtGui.QApplication.UnicodeUTF8))
        self.rdio_lock.setText(QtGui.QApplication.translate("User_admin_frame", "Lock", None, QtGui.QApplication.UnicodeUTF8))
        self.rdio_unlock.setText(QtGui.QApplication.translate("User_admin_frame", "Unlock", None, QtGui.QApplication.UnicodeUTF8))
        self.groupBox_2.setTitle(QtGui.QApplication.translate("User_admin_frame", "Add User", None, QtGui.QApplication.UnicodeUTF8))
        self.label_2.setText(QtGui.QApplication.translate("User_admin_frame", "User Name", None, QtGui.QApplication.UnicodeUTF8))
        self.label_3.setText(QtGui.QApplication.translate("User_admin_frame", "Password", None, QtGui.QApplication.UnicodeUTF8))
        self.chkbx_defGroup.setText(QtGui.QApplication.translate("User_admin_frame", "Add default group", None, QtGui.QApplication.UnicodeUTF8))
        self.btn_addUser.setText(QtGui.QApplication.translate("User_admin_frame", "Add User", None, QtGui.QApplication.UnicodeUTF8))
        self.groupBox_7.setTitle(QtGui.QApplication.translate("User_admin_frame", "Change User Password", None, QtGui.QApplication.UnicodeUTF8))
        self.label_4.setText(QtGui.QApplication.translate("User_admin_frame", "Selected user", None, QtGui.QApplication.UnicodeUTF8))
        self.label_7.setText(QtGui.QApplication.translate("User_admin_frame", "New password", None, QtGui.QApplication.UnicodeUTF8))
        self.btn_changePassword.setText(QtGui.QApplication.translate("User_admin_frame", "Change password", None, QtGui.QApplication.UnicodeUTF8))
        ######### set tool tips ##############
        self.text_userName.setToolTip(QtGui.QApplication.translate("Form", "pass the name of the user you want to add", None, QtGui.QApplication.UnicodeUTF8))
        self.text_password.setToolTip(QtGui.QApplication.translate("Form", "pass the password of the user you want to add", None, QtGui.QApplication.UnicodeUTF8))
        self.text_userSelecedName.setToolTip(QtGui.QApplication.translate("Form", "pass the user name", None, QtGui.QApplication.UnicodeUTF8))
        self.text_newPassword.setToolTip(QtGui.QApplication.translate("Form", "pass the new password you choosed", None, QtGui.QApplication.UnicodeUTF8))
        

    def showAllUsers(self):
        '''
        showAllUsers(self)
            return all users in my system
        '''
        self.listWidget_users.clear()
        usersList = self.userAdmin.listAllUsers()
        for user in usersList:
            self.listWidget_users.addItem(user)
    
    
    def addUser(self):
        '''
        addUser(self)
            this function add new user to the remote system
        '''
        user = str(self.text_userName.text())
        passwd = str(self.text_password.text())
        chooseDefaultGroup = str(self.combo_defaultGroup.currentText())
        defaultgroupchk = str(self.chkbx_defGroup.checkState())
        if (user != "") and (passwd != ""):
            if defaultgroupchk == "2":
                self.userAdmin.addUser(user, passwd, chooseDefaultGroup)
                QtGui.QMessageBox.information(self, "Information","the user "+user+" is added successfully\n with default group "+chooseDefaultGroup)
                self.text_userName.clear()
                self.text_password.clear()
            elif defaultgroupchk == "0":
                self.userAdmin.addUser(user, passwd)
                QtGui.QMessageBox.information(self, "Information","the user "+user+" is added successfully")
                self.text_userName.clear()
                self.text_password.clear()
        else:
            QtGui.QMessageBox.critical(self, "Exception","You must set user name you want to add and his password")
    
    
    def textChanged(self):
        currentUser = str(self.listWidget_users.currentItem().text())
        self.text_userSelecedName.setText(currentUser)
        
    def changePassword(self):
        '''
        changePassword(self)
            this function change the password of selected user
        '''
        currentUser = str(self.listWidget_users.currentItem().text())
        selectedUser = str(self.text_userSelecedName.setText(currentUser))
        newPass = str(self.text_newPassword.text())
        if (currentUser != "") and (newPass != ""):
            self.userAdmin.changeUserPasswd(currentUser , newPass)
            QtGui.QMessageBox.information(self, "Information","Password changed successfully")
        elif (selectedUser != "") and (newPass != ""):
            self.userAdmin.changeUserPasswd(selectedUser, newPass)
        else:
            QtGui.QMessageBox.critical(self, "Exception","you must select the user and enter his new password")
    
            
            
    def delUser(self):
        '''
        delUser(self)
            this function delete selected user
        '''
        currentUser = str(self.listWidget_users.currentItem().text())
        delHomechk = str(self.chkbx_delUserHome.checkState())
        if (delHomechk == "2") and (currentUser != ""):
            if self.userAdmin.delUser(currentUser,True):
                QtGui.QMessageBox.information(self, "Information","user "+currentUser+" deleted successfully \nwith his (home) directory!")
            else:
                QtGui.QMessageBox.critical(self, "Exception","Can't delete user now")
        
        elif currentUser != "":
            if self.userAdmin.delUser(currentUser):
                QtGui.QMessageBox.information(self,"Information", "user "+currentUser+" deleted successfully")
            else:
                QtGui.QMessageBox.critical(self, "Exception","Can't delete user now")
                
        else:
            QtGui.QMessageBox.critical(self, "Exception","select the user you want to delete")


    def performAction(self):
        '''
        performAction(self)
            this function perform lock/unlock operation on selected user
        '''
        currentUser = str(self.listWidget_users.currentItem().text())
        lockradio = self.rdio_lock.isChecked()
        unlockradio = self.rdio_unlock.isChecked()
        if (currentUser != "") and (lockradio == True):
            if self.userAdmin.lockUser(currentUser,"lock"):
                QtGui.QMessageBox.information(self, "Information","user "+currentUser+" is locked")
            else:
                QtGui.QMessageBox.critical(self, "Exception","Can't lock | lock the user now")
        elif (currentUser != "") and (unlockradio == True):
            if self.userAdmin.lockUser(currentUser,"unlock"):
                QtGui.QMessageBox.information(self, "Information","user "+currentUser+" is unlocked")
            else:
                QtGui.QMessageBox.critical(self, "Exception","Can't lock | unlock the user now")
        else:
            QtGui.QMessageBox.critical(self, "Exception","Can't perform action!")
    
